const http = require('http');
const querystring = require('querystring');

const server = http.createServer((req, res) => {
    const method = req.method;
    const url = req.url;
    const query = querystring.parse(url.split('?')[1]);

    // console.log("method：", method);
    // console.log("url：", url);
    // console.log("query:", query);

    const data = {
        method, 
        url,
        query
    }

    res.setHeader('Content-Type', 'application/json')
    if (method === 'GET') {
        res.end(
            JSON.stringify(data)
        )
    }

    if (method === 'POST') {
        let postData = '';
        req.on('data', chunk => {
            postData += chunk.toString();
        })
        req.on('end', () => {
            data.postData = postData;
            res.end(
                JSON.stringify(data)
            )
        })
    }

});

server.listen(9000, () => {
    console.log("server running at port 9000...");
})